package com.veetao.api.config;

import java.beans.PropertyVetoException;
import java.util.Properties;

import org.apache.ibatis.io.VFS;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import com.veetao.api.model.MyBatisConfigData;

@Configuration
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
public class DataSourceConfig {
	@Autowired
	private Environment env;
	
	@Autowired
	private MyBatisConfigData mybatisConfigData;
	



	@Bean
	public DataSource dataSource0() throws PropertyVetoException {
		PoolProperties p = new PoolProperties();
		p.setUrl(env.getProperty("datasource.url"));
		p.setDriverClassName(env.getProperty("datasource.driverClassName"));;
		p.setUsername(env.getProperty("datasource.username"));
		p.setPassword(env.getProperty("datasource.password"));
		p.setJmxEnabled(true);

		p.setTestWhileIdle(Boolean.parseBoolean(env.getProperty("datasource.testWhileIdle")));
		p.setTestOnBorrow(Boolean.parseBoolean(env.getProperty("datasource.testOnBorrow")));
		p.setValidationQuery("SELECT 1");
		p.setTestOnReturn(Boolean.parseBoolean(env.getProperty("datasource.testOnReturn")));
		p.setValidationInterval(30000);
		p.setTimeBetweenEvictionRunsMillis(30000);
		p.setMaxActive(Integer.parseInt(env.getProperty("datasource.maxActive")));
		p.setInitialSize(Integer.parseInt(env.getProperty("datasource.initialSize")));
		p.setMaxWait(10000);
		p.setMaxIdle(Integer.parseInt(env.getProperty("datasource.maxIdle")));
		p.setRemoveAbandonedTimeout(60);
		p.setMinEvictableIdleTimeMillis(30000);
		p.setMinIdle(Integer.parseInt(env.getProperty("datasource.minIdle")));
		p.setLogAbandoned(false);
		p.setRemoveAbandoned(true);

		p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
				+ "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");

		return new DataSource(p);
	}

	@Bean(name="sqlSessionFactoryBean1")
	public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
		final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(dataSource0());
		sessionFactory.setVfs(SpringBootVFS.class);
//		VFS.addImplClass(SpringBootVFS.class);
		sessionFactory.setTypeAliasesPackage("com.veetao.api.model," + mybatisConfigData.getTypeAliasesPackage());
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		Resource[] resourcesFramework = resolver.getResources("classpath:com/veetao/api/dao/mapper/Mapper*.xml");
		Resource[] resourcesCustom = resolver.getResources(mybatisConfigData.getMapperLocations());
		
		Resource[] allResources = new Resource[resourcesFramework.length + resourcesCustom.length];
		System.arraycopy(resourcesFramework, 0,  allResources, 0, resourcesFramework.length);
		System.arraycopy(resourcesCustom, 0,  allResources, resourcesFramework.length, resourcesCustom.length);
		sessionFactory.setMapperLocations(allResources);
		
		Properties p = new Properties();
		p.setProperty("logImpl", "SLF4J");
		sessionFactory.setConfigurationProperties(p);
		return sessionFactory;
	}

	public Environment getEnv() {
		return env;
	}

	public void setEnv(Environment env) {
		this.env = env;
	}


	public DataSourceConfig() {
		super();
	}


}